<!--
 * @FilePath     : /study_code/layui/11-16.html
 * @Description  : .maxTips() 函数：监听 多选 select 超出最大值时的钩子调用。
 * @Date         : 2025-04-09 11:36:46
 * @Author       : Fang YingGui
 * @LastEditors  : fangyinggui 18715384437@163.com
 * @LastEditTime : 2025-04-09 14:57:02
-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>documentName</title>
    <link rel="stylesheet" href="/layui/dist/css/layui.css" />
    <link rel="stylesheet" href="/layui/extends_modules/formSelects-v4/formSelects-v4.css" />
  </head>
  <body>
    <div>
      <select name="city" xm-select="select1" xm-select-max="3">
        <option value="1" disabled="disabled">北京</option>
        <option value="2" selected="selected">上海</option>
        <option value="3">广州</option>
        <option value="4" selected="selected">深圳</option>
        <option value="5">天津</option>
      </select>
    </div>

    <script src="/layui/dist/layui.js"></script>
    <script>
      layui
        .config({
          base: '/layui/extends_modules/', // 配置 layui 第3方扩展模块存放的基础目录
        })
        .extend({
          formSelects: '/formSelects-v4/formSelects-v4',
        })
        .use(['jquery', 'formSelects'], function () {
          var $ = layui.jquery,
            formSelects = layui.formSelects

          /**
           * @brief: 监听 多选 select 超出最大值时的钩子调用。
           * @param {*} id xm-select 属性对应的值
           * @param {*} function: 调用的钩子函数
           * @param {*} id: xm-select 属性对应的值
           * @param {*} vals: 已选中子项的 数据集合
           * @param {*} val: 当前点击项的值
           * @param {*} max: 最大可选数（即 xm-select-max 属性对应的值）
           * @return {*}
           */
          // .maxTips(id, callback(id, vals, val, max))
          formSelects.maxTips('select1', function (id, vals, val, max) {
            console.log(1, id) //
            console.log(2, vals)
            /*
            [
              { name: '北京', value: '1', disabled: true },
              { name: '上海', value: '2', disabled: false },
              { name: '广州', value: '3', disabled: false },
              { name: '深圳', value: '4', disabled: false }
            ]
            */
            console.log(3, val)// { name: '天津', value: '5', disabled: false }
            console.log(4, max) // 3
            alert('选超了...')
          })
        })
    </script>
  </body>
</html>
